package com.serookie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.serookie.entity.Goods;
import com.serookie.entity.GoodsCategory;
import com.serookie.entity.Result;
import com.serookie.service.GoodsService;
import com.serookie.utils.CosUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author kevintam
 * @version 1.0
 * @date 2021/5/13 0:48
 */
@RestController
@Api(value="商品接口",tags={"商品接口"})
@RequestMapping("/admin/goods")
public class GoodsController{
    @Value("${tencent.path}")
    private String IMAGE_PATH;
    @Autowired
    private GoodsService goodsService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/all")
    @ApiOperation("查询所有的商品")
    public Result all(){
        List<Goods> all = goodsService.all();
        return Result.ok().message("查询成功").data("all",all);
    }

    /**
     * 根据id进行查询
     * @param goodsId
     * @return
     */
    @GetMapping("/info/{goodsId}")
    @ApiOperation("根据id进行查询商品")
    public Result infoById(@PathVariable("goodsId") Integer goodsId){
        Goods goods = goodsService.GoodsById(goodsId);
        return Result.ok().message("查询成功").data("data",goods);
    }
    @PostMapping("/del/{goodsId}")
    @ApiOperation("根据id进行删除商品")
    public Result delId(@PathVariable("goodsId") Integer goodsId){
        Boolean flag = goodsService.deleteGoods(goodsId);
        if(flag){
            return Result.ok().message("操作成功");
        }
        return Result.error().message("操作失败");
    }
    @GetMapping("/count")
    @ApiOperation("查询所有的商品记录数")
    public Result count(){
        Integer integer = goodsService.goodsCount();
        return Result.ok().message("操作成功").data("count",integer);
    }
    /**
     * 模糊查询
     * @param goodsName
     * @return
     */
    @GetMapping("/info")
    @ApiOperation("根据name进行查询商品")
    public Result infoByName(String goodsName){
        System.out.println(goodsName);
        List<Goods> goods = goodsService.GoodsByName(goodsName);
        return Result.ok().message("查询成功").data("data",goods);
    }

    /**
     * 新增商品
     * @param goods
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增商品")
    public Result save(Goods goods){
        Boolean flag = goodsService.addGoods(goods);
        if(flag){
            return Result.ok().message("查询成功");
        }
        return Result.error().message("查询失败");
    }
    @GetMapping("/catGoods/{catId}")
    @ApiOperation("根据分类id查询商品")
    public Result getByCatId(@PathVariable("catId") Integer catId){
        System.out.println(catId);
        List<Goods> all=null;
        try {
            all = goodsService.getByCatId(catId);
            return Result.ok().message("操作成功").data("data",all);
        } catch ( Exception e ) {
            e.printStackTrace();
        }
        return Result.error().message("操作失败");
    }
    /**
     * 修改商品分类
     * @param goods
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("修改商品")
    public Result update(Goods goods){
        boolean flag = goodsService.updateGoods(goods);
        if(flag){
            return Result.ok().message("修改成功");
        }
        return Result.error().message("修改失败");
    }
    @ApiOperation("上传商品图")
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public Result upload(@RequestParam("file") MultipartFile mulipartFilet) throws IOException {
        String fh="";
        String fileName = mulipartFilet.getOriginalFilename();
        //判断有无后缀
        if (fileName.lastIndexOf(".")<0) {
            return Result.ok().message("上传图片格式不正确!");
        }
        //获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));

        if(!prefix.equalsIgnoreCase(".jpg")&&!prefix.equalsIgnoreCase(".jpeg")
                && !prefix.equalsIgnoreCase(".svg")&&!prefix.equalsIgnoreCase(".png")){
            return Result.ok().message("上传格式不正确!");
        }
        //使用uuid作为文件名，防止生成的临时文件重复
        final File excelFile =File.createTempFile("imagesFile-" + System.currentTimeMillis(), prefix);
        //将Multifile转换成File
        mulipartFilet.transferTo(excelFile);

        //调用腾讯云工具上传文件
        String imageName = CosUtils.uploadfile(excelFile, "mall");
        //存入图片名称，用于网页显示
        // model.addAttribute("imageName", imageName);
        return Result.ok().message("上传成功").data("data",IMAGE_PATH+imageName);
    }

    /**
     * 删除临时文件
     *
     * @param files 临时文件，可变参数
     */
    private void deleteFile(File... files) {
        for (File file : files) {
            if (file.exists()) {
                file.delete();
            }
        }
    }
}
